<!DOCTYPE html>
<!--
Copyright (c) 2014 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->
<link rel="import" href="/tracing/base/settings.html">
<script>
'use strict';

tr.b.unittest.testSuite(function() {
  function assertSettingIs(expectedValue, key) {
    assert.strictEqual(tr.b.Settings.get(key), expectedValue);
  }

  // Old settings versions used to stringify objects instead of putting them
  // into JSON. This test makes sure that these old settings yield the default
  // value instead of strings.
  test('oldStyleSettingYieldsDefaultValue', function() {
    const storage = tr.b.Settings.getAlternativeStorageInstance();
    storage.setItem(tr.b.Settings.namespace_('key'), 'hello world');

    assert.strictEqual(tr.b.Settings.get('key', 'value'), 'value');
  });

  test('setGetString', function() {
    const settings = new tr.b.Settings();
    settings.set('my_key', 'my_val');
    assert.strictEqual(settings.get('my_key'), 'my_val');
    // tr.b.Settings() is a singleton
    assert.strictEqual(tr.b.Settings().get('my_key'), 'my_val');
  });

  test('setGetNumber', function() {
    const settings = new tr.b.Settings();
    settings.set('my_key', 5);
    assertSettingIs(5, 'my_key');
  });

  test('setGetBool', function() {
    const settings = new tr.b.Settings();
    settings.set('my_key', false);
    assertSettingIs(false, 'my_key');
  });

  test('setGetObject', function() {
    const settings = new tr.b.Settings();
    settings.set('my_key', {'hello': 5});
    assert.deepEqual(settings.get('my_key'), {'hello': 5});
  });

  test('setInvalidObject', function() {
    const settings = new tr.b.Settings();
    const obj = {'hello': undefined};
    obj.hello = obj;
    assert.throws(function() {
      settings.set('my_key', obj);
    });
  });

  test('setUndefined', function() {
    const settings = new tr.b.Settings();
    assert.throws(function() {
      settings.set('my_key', undefined);
    });
  });

  test('getUnset', function() {
    const settings = new tr.b.Settings();
    // Undefined should be returned if value isn't set.
    assertSettingIs(undefined, 'my_key');
  });

  test('getDefault', function() {
    const settings = new tr.b.Settings();
    // default_val should be returned if value isn't set.
    assert.strictEqual(settings.get('my_key', 'default_val'), 'default_val');
  });

  test('setGetPrefix', function() {
    const settings = new tr.b.Settings();
    settings.set('key_a', 'foo', 'my_prefix');
    assert.strictEqual(settings.get('key_a', undefined, 'my_prefix'), 'foo');
    assert.strictEqual(settings.get('key_a', 'bar', 'my_prefix'), 'foo');
    assert.isUndefined(settings.get('key_a'));
    assert.strictEqual(settings.get('key_a', 'bar'), 'bar');
  });

  test('keys', function() {
    const settings = new tr.b.Settings();
    settings.set('key_a', 'foo');
    settings.set('key_b', 'bar');
    settings.set('key_c', 'baz');
    assert.sameMembers(settings.keys(), ['key_a', 'key_b', 'key_c']);
  });

  test('keysPrefix', function() {
    const settings = new tr.b.Settings();
    settings.set('key_a', 'foo', 'prefix1');
    settings.set('key_b', 'bar', 'prefix1');
    settings.set('key_c', 'baz', 'prefix1');
    settings.set('key_a', 'foo', 'prefix2');
    settings.set('key_b', 'bar', 'prefix2');
    settings.set('key_C', 'baz', 'prefix2');
    assert.sameMembers(settings.keys('prefix1'), ['key_a', 'key_b', 'key_c']);
    assert.sameMembers(settings.keys('prefix2'), ['key_C', 'key_a', 'key_b']);
    assert.sameMembers(
        settings.keys(),
        ['prefix1.key_a', 'prefix1.key_b', 'prefix1.key_c',
          'prefix2.key_C', 'prefix2.key_a', 'prefix2.key_b']);
  });
});
</script>
